using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Arcanoid
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        
        private Rectangle _viewGameField;
        private GameObject _platform;
        private Texture2D _background;
        
        private int _brickOnWidth = 10;
        private int _brickOnHeight = 6;
        private Texture2D _brickSprite;
        private GameObject[,] _bricks;   //array of bricks

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }


        protected override void Initialize()
        {
            base.Initialize();
            //graphics.PreferredBackBufferWidth = 900;
            //graphics.PreferredBackBufferHeight = 600;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            Window.Title = "Arcanoid Game";
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            _viewGameField = new Rectangle(0, 0, this.Window.ClientBounds.Width, this.Window.ClientBounds.Height);
            _background = Content.Load<Texture2D>(@"Textures\background");
            _platform = new GameObject(Content.Load<Texture2D>(@"Textures\platform"));
            _platform.Position = new Vector2((_viewGameField.Width - _platform.Width)/2,
                                             _viewGameField.Height - _platform.Height - 20);
            _brickSprite = Content.Load<Texture2D>(@"Textures\brick");
            _bricks = new GameObject[_brickOnWidth,_brickOnHeight];

            for (int i=0; i<_brickOnWidth; i++)
            {
                for(int j=0; j<_brickOnHeight; j++)
                {
                    _bricks[i, j] = new GameObject(_brickSprite)
                                        {
                                            Position = new Vector2(i*2 + 150, j + 30)
                                        };
                }
            }
        }


        protected override void UnloadContent()
        {
            
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            KeyboardState keyboardState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();

            if (keyboardState.IsKeyDown(Keys.Right))
            {
                _platform.Position.X += 5f;
            }

            if (keyboardState.IsKeyDown(Keys.Left))
            {
                _platform.Position.X -= 5f;
            }

            _platform.Position.X = MathHelper.Clamp(_platform.Position.X, 0, 650);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(_background, _viewGameField, Color.White);
            spriteBatch.Draw(_platform.Sprite, _platform.Position, Color.White);

            foreach (var brick in _bricks)
            {
                if (brick.IsAlive)
                {
                    spriteBatch.Draw(brick.Sprite, brick.Position, Color.White);
                }
            }



            spriteBatch.End();
                 
            

            base.Draw(gameTime);
        }
    }
}
